home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlsyn.1 < prev    next >
Text File  |  1995-07-25  |  15KB  |  397 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlsyn - Perl syntax
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.           A Perl script consists of a sequence of declarations and
  13.           statements.  The only things that need to be declared in
  14.           Perl are report formats and subroutines.  See the sections
  15.           below for more information on those declarations.  All
  16.           uninitialized user-created objects are assumed to start with
  17.           a null or 0 value until they are defined by some explicit
  18.           operation such as assignment.  (Though you can get warnings
  19.           about the use of undefined values if you like.)  The
  20.           sequence of statements is executed just once, unlike in sssseeeedddd
  21.           and aaaawwwwkkkk scripts, where the sequence of statements is
  22.           executed for each input line.  While this means that you
  23.           must explicitly loop over the lines of your input file (or
  24.           files), it also means you have much more control over which
  25.           files and which lines you look at.  (Actually, I'm lying--it
  26.           is possible to do an implicit loop with either the ----nnnn or ----pppp
  27.           switch.  It's just not the mandatory default like it is in
  28.           sssseeeedddd and aaaawwwwkkkk.)
  29.  
  30.           Perl is, for the most part, a free-form language.  (The only
  31.           exception to this is format declarations, for obvious
  32.           reasons.) Comments are indicated by the "#" character, and
  33.           extend to the end of the line.  If you attempt to use /* */
  34.           C-style comments, it will be interpreted either as division
  35.           or pattern matching, depending on the context, and C++ //
  36.           comments just look like a null regular expression, So don't
  37.           do that.
  38.  
  39.           A declaration can be put anywhere a statement can, but has
  40.           no effect on the execution of the primary sequence of
  41.           statements--declarations all take effect at compile time.
  42.           Typically all the declarations are put at the beginning or
  43.           the end of the script.
  44.  
  45.           As of Perl 5, declaring a subroutine allows a subroutine
  46.           name to be used as if it were a list operator from that
  47.           point forward in the program.  You can declare a subroutine
  48.           without defining it by saying just
  49.  
  50.               sub myname;
  51.               $me = myname $0             or die "can't get myname";
  52.  
  53.           Note that it functions as a list operator though, not a
  54.           unary operator, so be careful to use or instead of || there.
  55.  
  56.           Subroutines declarations can also be imported by a use
  57.           statement.
  58.  
  59.           Also as of Perl 5, a statement sequence may contain
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  71.  
  72.  
  73.  
  74.           declarations of lexically scoped variables, but apart from
  75.           declaring a variable name, the declaration acts like an
  76.           ordinary statement, and is elaborated within the sequence of
  77.           statements as if it were an ordinary statement.
  78.  
  79.           SSSSiiiimmmmpppplllleeee ssssttttaaaatttteeeemmmmeeeennnnttttssss
  80.  
  81.           The only kind of simple statement is an expression evaluated
  82.           for its side effects.  Every simple statement must be
  83.           terminated with a semicolon, unless it is the final
  84.           statement in a block, in which case the semicolon is
  85.           optional.  (A semicolon is still encouraged there if the
  86.           block takes up more than one line, since you may add another
  87.           line.) Note that there are some operators like eval {} and
  88.           do {} that look like compound statements, but aren't
  89.           (they're just TERMs in an expression), and thus need an
  90.           explicit termination if used as the last item in a
  91.           statement.
  92.  
  93.           Any simple statement may optionally be followed by a _S_I_N_G_L_E
  94.           modifier, just before the terminating semicolon (or block
  95.           ending).  The possible modifiers are:
  96.  
  97.               if EXPR
  98.               unless EXPR
  99.               while EXPR
  100.               until EXPR
  101.  
  102.           The if and unless modifiers have the expected semantics,
  103.           presuming you're a speaker of English.  The while and until
  104.           modifiers also have the usual "while loop" semantics
  105.           (conditional evaluated first), except when applied to a do-
  106.           BLOCK (or to the now-deprecated do-SUBROUTINE statement), in
  107.           which case the block executes once before the conditional is
  108.           evaluated.  This is so that you can write loops like:
  109.  
  110.               do {
  111.                   $_ = <STDIN>;
  112.                   ...
  113.               } until $_ eq ".\n";
  114.  
  115.           See the do entry in the _p_e_r_l_f_u_n_c manpage.  Note also that
  116.           the loop control statements described later will _N_O_T work in
  117.           this construct, since modifiers don't take loop labels.
  118.           Sorry.  You can always wrap another block around it to do
  119.           that sort of thing.)
  120.  
  121.           CCCCoooommmmppppoooouuuunnnndddd ssssttttaaaatttteeeemmmmeeeennnnttttssss
  122.  
  123.           In Perl, a sequence of statements that defines a scope is
  124.           called a block.  Sometimes a block is delimited by the file
  125.           containing it (in the case of a required file, or the
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  137.  
  138.  
  139.  
  140.           program as a whole), and sometimes a block is delimited by
  141.           the extent of a string (in the case of an eval).
  142.  
  143.           But generally, a block is delimited by curly brackets, also
  144.           known as braces.  We will call this syntactic construct a
  145.           BLOCK.
  146.  
  147.           The following compound statements may be used to control
  148.           flow:
  149.  
  150.               if (EXPR) BLOCK
  151.               if (EXPR) BLOCK else BLOCK
  152.               if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
  153.               LABEL while (EXPR) BLOCK
  154.               LABEL while (EXPR) BLOCK continue BLOCK
  155.               LABEL for (EXPR; EXPR; EXPR) BLOCK
  156.               LABEL foreach VAR (ARRAY) BLOCK
  157.               LABEL BLOCK continue BLOCK
  158.  
  159.           Note that, unlike C and Pascal, these are defined in terms
  160.           of BLOCKs, not statements.  This means that the curly
  161.           brackets are _r_e_q_u_i_r_e_d--no dangling statements allowed.  If
  162.           you want to write conditionals without curly brackets there
  163.           are several other ways to do it.  The following all do the
  164.           same thing:
  165.  
  166.               if (!open(FOO)) { die "Can't open $FOO: $!"; }
  167.               die "Can't open $FOO: $!" unless open(FOO);
  168.               open(FOO) or die "Can't open $FOO: $!";     # FOO or bust!
  169.               open(FOO) ? 'hi mom' : die "Can't open $FOO: $!";
  170.                                   # a bit exotic, that last one
  171.  
  172.           The if statement is straightforward.  Since BLOCKs are
  173.           always bounded by curly brackets, there is never any
  174.           ambiguity about which if an else goes with.  If you use
  175.           unless in place of if, the sense of the test is reversed.
  176.  
  177.           The while statement executes the block as long as the
  178.           expression is true (does not evaluate to the null string or
  179.           0 or "0").  The LABEL is optional, and if present, consists
  180.           of an identifier followed by a colon.  The LABEL identifies
  181.           the loop for the loop control statements next, last, and
  182.           redo (see below).  If there is a continue BLOCK, it is
  183.           always executed just before the conditional is about to be
  184.           evaluated again, just like the third part of a for loop in
  185.           C.  Thus it can be used to increment a loop variable, even
  186.           when the loop has been continued via the next statement
  187.           (which is similar to the C continue statement).
  188.  
  189.           If the word while is replaced by the word until, the sense
  190.           of the test is reversed, but the conditional is still tested
  191.           before the first iteration.
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  203.  
  204.  
  205.  
  206.           In either the if or the while statement, you may replace
  207.           "(EXPR)" with a BLOCK, and the conditional is true if the
  208.           value of the last statement in that block is true.  (This
  209.           feature continues to work in Perl 5 but is deprecated.
  210.           Please change any occurrences of "if BLOCK" to "if (do
  211.           BLOCK)".)
  212.  
  213.           The C-style for loop works exactly like the corresponding
  214.           while loop:
  215.  
  216.               for ($i = 1; $i < 10; $i++) {
  217.                   ...
  218.               }
  219.  
  220.           is the same as
  221.  
  222.               $i = 1;
  223.               while ($i < 10) {
  224.                   ...
  225.               } continue {
  226.                   $i++;
  227.               }
  228.  
  229.           The foreach loop iterates over a normal list value and sets
  230.           the variable VAR to be each element of the list in turn.
  231.           The variable is implicitly local to the loop (unless
  232.           declared previously with my), and regains its former value
  233.           upon exiting the loop.  The foreach keyword is actually a
  234.           synonym for the for keyword, so you can use foreach for
  235.           readability or for for brevity.  If VAR is omitted, $_ is
  236.           set to each value.  If ARRAY is an actual array (as opposed
  237.           to an expression returning a list value), you can modify
  238.           each element of the array by modifying VAR inside the loop.
  239.           Examples:
  240.  
  241.               for (@ary) { s/foo/bar/; }
  242.  
  243.               foreach $elem (@elements) {
  244.                   $elem *= 2;
  245.               }
  246.  
  247.               for ((10,9,8,7,6,5,4,3,2,1,'BOOM')) {
  248.                   print $_, "\n"; sleep(1);
  249.               }
  250.  
  251.               for (1..15) { print "Merry Christmas\n"; }
  252.  
  253.               foreach $item (split(/:[\\\n:]*/, $ENV{'TERMCAP'})) {
  254.                   print "Item: $item\n";
  255.               }
  256.  
  257.           A BLOCK by itself (labeled or not) is semantically
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  269.  
  270.  
  271.  
  272.           equivalent to a loop that executes once.  Thus you can use
  273.           any of the loop control statements in it to leave or restart
  274.           the block.  The continue block is optional.  This construct
  275.           is particularly nice for doing case structures.
  276.  
  277.               SWITCH: {
  278.                   if (/^abc/) { $abc = 1; last SWITCH; }
  279.                   if (/^def/) { $def = 1; last SWITCH; }
  280.                   if (/^xyz/) { $xyz = 1; last SWITCH; }
  281.                   $nothing = 1;
  282.               }
  283.  
  284.           There is no official switch statement in Perl, because there
  285.           are already several ways to write the equivalent.  In
  286.           addition to the above, you could write
  287.  
  288.               SWITCH: {
  289.                   $abc = 1, last SWITCH  if /^abc/;
  290.                   $def = 1, last SWITCH  if /^def/;
  291.                   $xyz = 1, last SWITCH  if /^xyz/;
  292.                   $nothing = 1;
  293.               }
  294.  
  295.           (That's actually not as strange as it looks one you realize
  296.           that you can use loop control "operators" within an
  297.           expression,  That's just the normal C comma operator.)
  298.  
  299.           or
  300.  
  301.               SWITCH: {
  302.                   /^abc/ && do { $abc = 1; last SWITCH; };
  303.                   /^def/ && do { $def = 1; last SWITCH; };
  304.                   /^xyz/ && do { $xyz = 1; last SWITCH; };
  305.                   $nothing = 1;
  306.               }
  307.  
  308.           or formatted so it stands out more as a "proper" switch
  309.           statement:
  310.  
  311.               SWITCH: {
  312.                   /^abc/      && do {
  313.                                       $abc = 1;
  314.                                       last SWITCH;
  315.                                  };
  316.  
  317.                   /^def/      && do {
  318.                                       $def = 1;
  319.                                       last SWITCH;
  320.                                  };
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSYYYYNNNN((((1111))))
  335.  
  336.  
  337.  
  338.                   /^xyz/      && do {
  339.                                       $xyz = 1;
  340.                                       last SWITCH;
  341.                                   };
  342.                   $nothing = 1;
  343.               }
  344.  
  345.           or
  346.  
  347.               SWITCH: {
  348.                   /^abc/ and $abc = 1, last SWITCH;
  349.                   /^def/ and $def = 1, last SWITCH;
  350.                   /^xyz/ and $xyz = 1, last SWITCH;
  351.                   $nothing = 1;
  352.               }
  353.  
  354.           or even, horrors,
  355.  
  356.               if (/^abc/)
  357.                   { $abc = 1 }
  358.               elsif (/^def/)
  359.                   { $def = 1 }
  360.               elsif (/^xyz/)
  361.                   { $xyz = 1 }
  362.               else
  363.                   { $nothing = 1 }
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.